﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC_Uthorization.API.Application.Command.UserComand;
using RBAC_Uthorization.API.Application.Command.UserInfoCommand;
using RBAC_Uthorization.API.DTO;
using RBAC_Uthorization.API.DTO.Request;
using RBAC_Uthorization.API.DTO.Request.UserInfo;
using RBAC_Uthorization.Domain;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace RBAC_Uthorization.API.Controllers
{
    [Route("api/[controller]")]
    [Authorize]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<UserController> _logger;

        public UserController(IMediator mediator,  ILogger<UserController> logger)
        {
            _mediator = mediator;
         
            _logger = logger;
        }
        /// <summary>
        /// 公司主账号创建
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("offCreate")]
        public async Task<ApiResult<int>> Create( UserInfoCreateDto dto)
        {
            if (!ModelState.IsValid)
            {
                return new ApiResult<int>() { Code = ResultCode.BadRequest, Message = "参数错误" };
            }
            //var sub = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
            //var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
            //var uri = HttpContext.User.FindFirst(ClaimTypes.Uri)?.Value;//获取头像
            //var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取名称
            //var claims = HttpContext.User.Claims;

         
            var command = new UserInfoCreateCommand()
            {
                //获取token的用户名 
                CreateName = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value,
                CreateDto = dto,
               
            };
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 用户创建
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("userCreate")]
        public async Task<IActionResult> UserCreate(UserloginDto dto)
        {
            if (!ModelState.IsValid)
            {
                return Ok(new ApiResult<int>() { Code = ResultCode.BadRequest, Message = "参数错误" });
            }
            var command = new UserloginCreateCommand()
            {
                //HttpContext.User.FindFirst(ClaimTypes.Name)?.Value
                //HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sub)?.Value
                createname = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value,
                dto = dto,
                offlcid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,
                sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value
            };
            return Ok(await _mediator.Send(command));
        }
        /// <summary>
        /// 用户修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut("userUpdate")]
        public async Task<IActionResult> UserUpdate(UserInfoUpdateDto dto)
        {
            if (!ModelState.IsValid)
            {
                return Ok(new ApiResult<int>() { Code = ResultCode.BadRequest, Message = "参数错误" });
            }
            var command = new USerInfoUpdateCommand()
            {
                //HttpContext.User.FindFirst(ClaimTypes.Name)?.Value
                //HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sub)?.Value
                Createby = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value,

                UpdateDto = dto,
                office_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,
                sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value
            };
            return Ok(await _mediator.Send(command));
        }
        /// <summary>
        /// 用户删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("userDelete")]
        public async Task<IActionResult> UserDelete(int[] id)
        {
            if (!ModelState.IsValid)
            {
                return Ok(new ApiResult<int>() { Code = ResultCode.BadRequest, Message = "参数错误" });
            }
            var command = new USerInfoDeleteCommand()
            {
                //HttpContext.User.FindFirst(ClaimTypes.Name)?.Value
                //HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sub)?.Value
                Createby = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value,
                id = id,
                office_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,
                sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value
            };
            return Ok(await _mediator.Send(command));
        }
    }
}
